package net.zjitc.security.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import net.zjitc.common.ResponseResult;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class CustomizeAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        ResponseResult<Object> result = new ResponseResult<>();
        //设置响应头
        response.setContentType("application/json;charset=utf-8");

        result.setError("请先登录！");
        //认证信息存到redis........

        //restult转json
        response.getWriter().write(new ObjectMapper().writeValueAsString(result));
    }
}
